/* *********************************************************************
	AUTHOR: QH_SOLEDAD
	DATE: 21-11-2010
	PURPOSE: FRAMEBUFFER
************************************************************************ */
#include "head.h"
#include <dirent.h>
#define NUMBER 24
#define PATH "./picture/"

struct pic_node
{
	char pic_buf[50];
	struct pic_node *next;

};

typedef struct pic_node pic_name;

fb_info my_fb; /* Screen information */
int length;

pic_name *head = NULL;

pic_name *newnode_t(char *buf);
pic_name *insert_t(char *buf);
void print_t();
void search_t(char *path);

pic_name *newnode_t(char *buf)
{
	pic_name *p;

	p = (pic_name *)malloc(sizeof(pic_name));
	if(p == NULL)
		return NULL;
	strncpy(p -> pic_buf, buf, strlen(buf) + 1);
	p -> next = NULL;
	return p;
}

pic_name *insert_t(char *buf)
{
	pic_name *p, *q;

	p = newnode_t(buf);

	if(p == NULL)
	{
		printf("New error!\n");
		return NULL;
	}

	if(head == NULL)
		head = p;
	else
	{
		for(q = head; q -> next != NULL; q = q -> next);
		q -> next = p;
	}

	return head;
}

void print_t()
{
	int h_t = my_fb.h / 2;
	int w_t = my_fb.w / 2;

	pic_name *p;
	
	int cmd = 0;
	int x_t = 0, y_t = 0;
	int mytime = 100;
	while(1)
	{			
		for(p = head; p != NULL && mytime != 0; p = p -> next, mytime -= 1)
		{
			system("clear");
			srand(time(NULL));
			usleep(10000);

			cmd = rand() % NUMBER + 1;
			switch(cmd)
			{
			case 1:
				printf("Designer %d\n", cmd);
				disp_jpeg_up(p -> pic_buf);
				system("clear");
				break;

			case 2:	
				printf("Designer %d\n", cmd);
				disp_jpeg_down(p -> pic_buf);
				system("clear");
				break;
	
			case 3:
				printf("Designer %d\n", cmd);
				disp_jpeg_c(p -> pic_buf, 1000, w_t, 0);
				system("clear");
				break;


			case 4:
				printf("Designer %d\n", cmd);
				disp_jpeg_c(p -> pic_buf, h_t, w_t, 0);	
				system("clear");
				break;



			case 5:
				printf("Designer %d\n", cmd);
				disp_jpeg_d(p -> pic_buf, h_t, w_t, 0);	
				system("clear");
				break;

			case 6:
				printf("Designer %d\n", cmd);
				disp_jpeg_2(p -> pic_buf);
				system("clear");
				break;

			case 7:
				printf("Designer %d\n", cmd);
				disp_jpeg_d(p -> pic_buf, w_t, h_t, 5);
				system("clear");
				break;
			
			case 8:
				printf("Designer %d\n", cmd);
				disp_jpeg_left(p -> pic_buf);
				system("clear");
				break;

			case 9:
				printf("Designer %d\n", cmd);
				disp_jpeg_right(p -> pic_buf);
				system("clear");
				break;

			case 10: 
				printf("Designer %d\n", cmd);
				disp_jpeg_to_middle(p -> pic_buf);
				system("clear");
				break;

			case 11: 
				printf("Designer %d\n", cmd);
				disp_jpeg_middle_to(p -> pic_buf);
				system("clear");
				break;


			case 12: 
				printf("Designer %d\n", cmd);
				disp_jpeg_center(p -> pic_buf);
				system("clear");
				break;
	
			case 13: 
				printf("Designer %d\n", cmd);
				disp_jpeg_center_1(p -> pic_buf);
				system("clear");
				break;

			case 14: 
				printf("Designer %d\n", cmd);
				disp_jpeg_center_2(p -> pic_buf);
				system("clear");
				break;

			case 15: 
				printf("Designer %d\n", cmd);
				disp_jpeg_middle_side(p -> pic_buf);
				system("clear");
				break;

			case 16: 
				printf("Designer %d\n", cmd);
				disp_jpeg_style1(p -> pic_buf);
				system("clear");
				break;

			case 17: 
				printf("Designer %d\n", cmd);
				disp_jpeg_style2(p -> pic_buf);
				system("clear");
				break;

			case 18: 
				printf("Designer %d\n", cmd);
				disp_jpeg_style3(p -> pic_buf);
				system("clear");
				break;

			case 19: 
				printf("Designer %d\n", cmd);
				disp_jpeg_style4(p -> pic_buf);
				system("clear");
				break;

			case 20: 
				printf("Designer %d\n", cmd);
				disp_jpeg_style5(p -> pic_buf);
				system("clear");
				break;

			case 21:
				printf("Designer %d\n", cmd);
				x_t = rand() % (my_fb.w);
				y_t = rand() % (my_fb.h);
				disp_jpeg_d(p -> pic_buf, y_t, x_t, 0);
				system("clear");
				break;

			case 22:
				printf("Designer %d\n", cmd);
				disp_jpeg_d(p -> pic_buf, my_fb.h / 2, my_fb.w, 0);
				system("clear");
				break;

			case 23:
				printf("Designer %d\n", cmd);
				disp_jpeg_d(p -> pic_buf, my_fb.h / 2, 0, 0);
				system("clear");
				break;
				
			default:
				break;
			}	
				
		}
		break;
	}
}	

void search_t(char *path)
{

	char infile[50] = {0};
	struct dirent *ptr;
	DIR *dir;
	dir = opendir(path);

	while((ptr = readdir(dir)) != NULL)
	{
		if(strcmp(ptr -> d_name, ".") == 0 || strcmp(ptr -> d_name, "..") == 0)
			continue;
		sprintf(infile, PATH"%s", ptr -> d_name);

		insert_t(infile);
	}

	closedir(dir);
}
 
int main(int argc, char * argv[])
{
	length = (my_fb.w * my_fb.h * my_fb.bpp) / 8;

	if(init() < 0)
	{
		printf("Initialize fialed\n");
		return 1;
	}
	system("clear");

	search_t(PATH);
		
	print_t();

	printf("Over here\n");	
	
	munmap(my_fb.fbmem, length);
	return 0;
}

